<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Periode extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
        $this->load->model('periode_model');
    }

//end Main
    //load Grid
    function index() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
            $colModel['Nama_Periode'] = array('Periode', 200, TRUE, 'center', 1);
            $colModel['MULAI'] = array('Tanggal Mulai', 200, TRUE, 'center', 1);
            $colModel['SELESAI'] = array('Tanggal Selesai', 200, TRUE, 'center', 1);
            $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
			$colModel['Lihat_Data_Jalan'] = array('Lihat Data Jalan', 100, TRUE, 'center', 0);
			

            //$colModel['Delete'] = array('Delete',30,TRUE,'center',0);


            $gridParams = array(
                'width' => 'auto',
                'height' => 350,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Periode',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Hapus', 'delete', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_berkas
            $grid_js = build_grid_js('flex1', site_url("periode/grid_periode"), $colModel, 'Nama_Periode', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/periode/form';
			}

			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' periode?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/periode/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					}
				}

		}
		</script>
		";


            $data['js_grid'] = $grid_js;

            //rendering view
            //$data['title'] = 'Daftar Peserta';
            //$data['js_grid']=$grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_periode() {
        $valid_fields = array('Nama_Periode');
        $this->flexigrid->validate_post('Nama_Periode', 'asc', $valid_fields);
        $records = $this->periode_model->get_data_periode();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
		
		//status perioide
		$status_aktif = "";
		if ($row->AKTIF == 1) {
			$status_aktif = "" . base_url() . "images/grid/check.png'";
		}
		else{
			$status_aktif = "" . base_url() . "images/grid/unaktif.png'";
		}
			
			
		
            $record_items[] = array(
                $row->ID_PERIODE,
                $no = $no + 1,
                $row->NAMA_PERIODE,
                $row->MULAI,
                $row->SELESAI,
                //remove a hreff
               '<a href=\'' . base_url() . 'index.php/periode/edit/' . $row->ID_PERIODE . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> ',
				'<a href=\'' . base_url() . 'index.php/jalan_periode/index/' . $row->ID_PERIODE . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/folder.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    function form() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
            $data['content'] = $this->load->view('form_periode', $data, true);
            $this->load->view('main', $data);
        }
    }

    function add() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() == false)
			{
				$this->form();
			}
			else{
				$data = array(
					'NAMA_PERIODE' => $this->input->post('nama_periode'),
					'MULAI' => $this->input->post('tgl_mulai'),
					'SELESAI' => $this->input->post('tgl_selesai')
				);
				$this->periode_model->insert($data);
				redirect('periode');
			}
        }
    }

    public function edit($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'edit';
            $record_artikel = $this->periode_model->selectone($id);
            foreach ($record_artikel->result() as $artikel) {
                $data['id_periode'] = $artikel->ID_PERIODE;
                $data['nama_periode'] = $artikel->NAMA_PERIODE;
                $data['tgl_mulai'] = $artikel->MULAI;
                $data['tgl_selesai'] = $artikel->SELESAI;
            }
            //$data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
            $data['content'] = $this->load->view('form_periode', $data, true);
            $this->load->view('main', $data);
        }
    }

    function update($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
				if($this->cek_validasi() == false)
				{
					$this->edit($id);
				}
				else{
				$data = array(
					'NAMA_PERIODE' => $this->input->post('nama_periode'),
					'MULAI' => $this->input->post('tgl_mulai'),
					'SELESAI' => $this->input->post('tgl_selesai')
				);
				$this->periode_model->update($id, $data);
				redirect('periode');
			}
        }
    }

    function update_status_aktif($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
				//set nilai 0
				$data = array(
					'AKTIF' => 0
				);
				$this->periode_model->set_status_nol($data);
				
				//set nilai 1
				$data2 = array(
					'AKTIF' => 1
				);
				$this->periode_model->set_status_satu($id, $data2);
				$this->session->set_userdata('id_periode_aktif', $id);
				redirect('periode');
            }
    }


    function delete() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
            //$this->load->model('pengolahan/data_model');
            //$msg='akun berhasil dihapus';
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
					$sql = $this->db->query("SELECT COUNT(id_kelas) AS CEK FROM kelas WHERE id_periode = ".$id."");
					$cek = '';
					foreach ($sql->result() as $rows)
					{
						$cek = $rows->CEK;
					}
					
					
					$sql2 = $this->db->query("SELECT * FROM periode WHERE id_periode = ".$id."");
					$status_aktif;
					foreach ($sql2->result() as $rows)
					{
						$status_aktif = $rows->AKTIF;
					}
							
					$msg='';
					if($cek > 0 || $status_aktif == 1)
						{
							$msg ='Proses hapus gagal. Periode sedang aktif atau masih memiliki kelas.';
						}
					else
						{
							$this->periode_model->delete($id);
							$msg ='Data yang Anda pilih berhasil dihapus.';
						}
					$this->output->set_header($this->config->item('ajax_header'));
					$this->output->set_output($msg);
				
                    //$this->periode_model->delete($id);
                }//end if
            }//end foreach
        }
    }
	
	
	
	function cek_validasi() {
        // Setting Rules
        $this->form_validation->set_rules('nama_periode', 'Periode', 'required');
        $this->form_validation->set_rules('tgl_mulai', 'Tanggal Mulai', 'required');
        $this->form_validation->set_rules('tgl_selesai', 'Tanggal Selesai', 'required');
		
		
        //Setting Error Message
        $this->form_validation->set_message('required', 'Field %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Field %s harus diisi angka.');
		$this->form_validation->set_message('greater_than', 'Field %s harus diisi angka lebih dari 0');


        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }
	
	
	function generate_buku_santri($id_periode){
		$record_artikel = $this->periode_model->selectone($id_periode);
		foreach ($record_artikel->result() as $artikel) {
			$data['id_periode'] = $artikel->ID_PERIODE;
			$data['nama_periode'] = $artikel->NAMA_PERIODE;
			$data['tgl_mulai'] = $artikel->MULAI;
			$data['tgl_selesai'] = $artikel->SELESAI;
		}
		
		$data['data_buku_l'] = $this->periode_model->get_data_buku_l($id_periode);
		$data['data_buku_p'] = $this->periode_model->get_data_buku_p($id_periode);
		$this->load->view('buku_santri', $data);
	}

}

//end class

/* End of file welcome.php */
/* Location: ./system/application/controllers/main.php */